home *** CD-ROM | disk | FTP | other *** search
Wrap
Imports System.Xml Public Class ReaderWriterForm Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents txtOut As System.Windows.Forms.TextBox Friend WithEvents btnReader As System.Windows.Forms.Button Friend WithEvents btnReader2 As System.Windows.Forms.Button Friend WithEvents btnWriter As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.btnReader = New System.Windows.Forms.Button() Me.btnReader2 = New System.Windows.Forms.Button() Me.txtOut = New System.Windows.Forms.TextBox() Me.btnWriter = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'btnReader ' Me.btnReader.Location = New System.Drawing.Point(8, 24) Me.btnReader.Name = "btnReader" Me.btnReader.Size = New System.Drawing.Size(120, 40) Me.btnReader.TabIndex = 0 Me.btnReader.Text = "XmlTextReader" ' 'btnReader2 ' Me.btnReader2.Location = New System.Drawing.Point(8, 72) Me.btnReader2.Name = "btnReader2" Me.btnReader2.Size = New System.Drawing.Size(120, 40) Me.btnReader2.TabIndex = 1 Me.btnReader2.Text = "XmlTextReader 2" ' 'txtOut ' Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.txtOut.Location = New System.Drawing.Point(144, 16) Me.txtOut.Multiline = True Me.txtOut.Name = "txtOut" Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both Me.txtOut.Size = New System.Drawing.Size(368, 288) Me.txtOut.TabIndex = 2 Me.txtOut.Text = "" Me.txtOut.WordWrap = False ' 'btnWriter ' Me.btnWriter.Location = New System.Drawing.Point(8, 120) Me.btnWriter.Name = "btnWriter" Me.btnWriter.Size = New System.Drawing.Size(120, 40) Me.btnWriter.TabIndex = 3 Me.btnWriter.Text = "XmlTextWriter" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17) Me.ClientSize = New System.Drawing.Size(528, 317) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWriter, Me.txtOut, Me.btnReader2, Me.btnReader}) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "Form1" Me.Text = "XML Demo" Me.ResumeLayout(False) End Sub #End Region Sub OutText(ByVal s As String) txtOut.AppendText(s) txtOut.AppendText(ControlChars.CrLf) End Sub ' create an XmlReader Private Sub btnReader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReader.Click txtOut.Clear() Dim xtr As New XmlTextReader("pubs.xml") Do While xtr.Read Select Case xtr.NodeType Case XmlNodeType.Document ' The root element in the XML data Case XmlNodeType.Element ' An XML element If xtr.Name = "pub_name" Then xtr.Read() OutText(xtr.Value) End If Case XmlNodeType.EndElement ' A closing XML tag Case XmlNodeType.Attribute ' An attribute Case XmlNodeType.Text ' Text value Case XmlNodeType.CDATA ' A CDATA section End Select Loop xtr.Close() End Sub Private Sub btnReader2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReader2.Click txtOut.Clear() Dim xtr As New XmlTextReader("invoices.xml") Do While xtr.Read If xtr.NodeType = XmlNodeType.Element Then ' Display the name of current node. txtOut.AppendText("<" & xtr.Name) ' Display name and value of attributes, if any. If xtr.HasAttributes Then Do While xtr.MoveToNextAttribute() txtOut.AppendText(" " & xtr.Name & "='" & xtr.Value & "'") Loop End If ' Go back to the main element. xtr.MoveToElement() ' Print the ending / if this is an empty element. If xtr.IsEmptyElement Then txtOut.AppendText("/") ' Close the tag. OutText(">") End If Loop xtr.Close() End Sub ' Convert a semicolon-delimited text file into XML. Private Sub btnWriter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriter.Click ' Open the file, read its contents. Dim sr As New System.IO.StreamReader("employees.dat") Dim fileText As String = sr.ReadToEnd sr.Close() ' Create the output XML file. Dim xtw As New XmlTextWriter("employees.xml", System.Text.Encoding.UTF8) ' Indent tags by 2 characters. xtw.Formatting = Formatting.Indented xtw.Indentation = 2 ' Enclose attributes' values in double quotes. xtw.QuoteChar = """"c ' Create the following XML declaration for this XML document: ' <?xml version="1.0" standalone="yes" ?> xtw.WriteStartDocument(True) ' Add a comment. xtw.WriteComment("Data converted from employees.dat file") ' The root element is <Employees>. xtw.WriteStartElement("Employees") ' This regular expression defines a row of elements, and assigns a name ' to each group (that is, a field in the text row). Dim re As New System.Text.RegularExpressions.Regex("""(?<fname>[^""]+)"";""(?<lname>[^""]+)"";(?<bdate>[^;]+);""(?<addr>[^""]+)"";""(?<city>[^""]+)""") Dim ma As System.Text.RegularExpressions.Match ' This variable will provide a unique id for each employee. Dim id As Integer For Each ma In re.Matches(fileText) ' A new line has been found, increment employee ID. id += 1 ' write a new <Employee id="xxx"> element. xtw.WriteStartElement("Employee") xtw.WriteAttributeString("id", id.ToString) ' write fields as nested elements containing text. xtw.WriteElementString("firstName", ma.Groups("fname").Value) xtw.WriteElementString("lastName", ma.Groups("lname").Value) xtw.WriteElementString("birthDate", ma.Groups("bdate").Value) xtw.WriteElementString("address", ma.Groups("addr").Value) xtw.WriteElementString("city", ma.Groups("city").Value) ' Close the <Employee> element. xtw.WriteEndElement() Next ' close the root element (and all pending elements, if any) xtw.WriteEndDocument() xtw.Close() ' Read the file into the txtOut control. Dim sr2 As New System.IO.StreamReader("employees.xml") txtOut.Text = sr2.ReadToEnd sr2.Close() End Sub End Class